<?php


namespace App\Services\Home;

use App\Models\Category;
use Illuminate\Support\Facades\Http;

class CategoryService
{
    protected $category;
    public function __construct(Category $category){
        $this->category = $category;
    }


    /**
     * @param array $params
     * @return \Illuminate\Http\Client\Response
     */
    public function getCategories($params = []){
//        $categories = Category::all();
//        $tree = $this->init_categories($categories, 0);
//        if (!count($categories)){
//            $res = Http::get('http:product.app/');
//        }
        $response = Http::post('http://product.app/api/category');
//    return $router->app->version();
        return $response;
    }

    /**
     * 递归解析菜单结构
     * @param $menu
     * @return array
     */
    private function init_categories($data, $pId)
    {
        $tree = array();
        foreach ($data as $k => $v) {

            if ($v->parent_id == $pId) {
                $children = $this->init_categories($data, $v->id);
                if ($children){
                    $v->children = $children;
                }

                //排序
                $sort[] = $v->sort;
                $tree[] = $v;
                array_multisort($sort,SORT_NUMERIC,SORT_ASC,$tree);
                unset($data[$k]);
            }
        }
        return $tree;
    }

}
